package com.demo.petshop.controller;

import com.demo.petshop.entity.ProductReviews;
import com.demo.petshop.service.ProductReviewsService;
import com.demo.petshop.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品评价控制层
 */
@RestController
@RequestMapping("/product-reviews")
public class ProductReviewsController {

    @Autowired
    private ProductReviewsService productReviewsService;

    /**
     * 添加商品评价
     *
     * @param productReviews 商品评价信息
     * @return 添加结果
     */
    @PostMapping("/add")
    public Result add(@RequestBody ProductReviews productReviews) {
        return Result.success(productReviewsService.saveReviews(productReviews));
    }

    /**
     * 根据商品ID获取评价列表
     *
     * @param productId 商品ID
     * @return 评价列表
     */
    @GetMapping("/product/{productId}")
    public Result getByProductId(@PathVariable Integer productId) {
        List<ProductReviews> reviews = productReviewsService.getByProductId(productId);
        return Result.success(reviews);
    }

    /**
     * 根据订单ID获取评价
     *
     * @param orderId 订单ID
     * @return 评价信息
     */
    @GetMapping("/order/{orderId}")
    public Result getByOrderId(@PathVariable Integer orderId) {
        ProductReviews review = productReviewsService.getByOrderId(orderId);
        return Result.success(review);
    }

    /**
     * 根据用户ID获取评价列表
     *
     * @param userId 用户ID
     * @return 评价列表
     */
    @GetMapping("/user/{userId}")
    public Result getByUserId(@PathVariable Integer userId) {
        List<ProductReviews> reviews = productReviewsService.getByUserId(userId);
        return Result.success(reviews);
    }
}
